package com.yanqu.road.server.manger;

import com.yanqu.road.entity.platform.PlatformForbidden;
import com.yanqu.road.entity.servercenter.ForbiddenInfo;
import com.yanqu.road.logic.bussiness.servercenter.ForbiddenBussiness;
import com.yanqu.road.logic.bussiness.servercenter.PlatformUserIdInfoBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ForbiddenMgr extends TempMgr {

	private static List<String> forbiddenIpList;

	private static List<String> forbiddenMacList;

	private static List<String> forbiddenSpeakIpList;

	private static List<String> forbiddenSpeakMacList;

	private static List<ForbiddenInfo> needAddForbiddenList;

	private static List<ForbiddenInfo> needRemoveForbiddenList;

	private static Map<String, Map<Integer, PlatformForbidden>> forbiddenPlatformUserIdMap;

	private static Map<Long, Map<Integer, Long>> forbiddenCmdMap;

	public static Map<Long, Map<Integer, Long>> getForbiddenCmdMap() {
		return forbiddenCmdMap;
	}

	public boolean save() {
		List<ForbiddenInfo> tempAddForbiddenList = new ArrayList<>();
		synchronized (needAddForbiddenList) {
			tempAddForbiddenList.addAll(needAddForbiddenList);
			needAddForbiddenList = new ArrayList<>();
		}
		for (ForbiddenInfo forbiddenInfo : tempAddForbiddenList) {
			ForbiddenBussiness.addForbiddenInfo(forbiddenInfo);
		}

		List<ForbiddenInfo> tempRemoveForbiddenList = new ArrayList<>();
		synchronized (needRemoveForbiddenList) {
			tempRemoveForbiddenList.addAll(needRemoveForbiddenList);
			needRemoveForbiddenList = new ArrayList<>();
		}
		for (ForbiddenInfo forbiddenInfo : tempRemoveForbiddenList) {
			ForbiddenBussiness.deleteForbiddenInfo(forbiddenInfo);
		}
		return true;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean reloadData() {
		forbiddenIpList = new ArrayList<>();
		forbiddenMacList = new ArrayList<>();
		forbiddenSpeakIpList = new ArrayList<>();
		forbiddenSpeakMacList = new ArrayList<>();
		needAddForbiddenList = new ArrayList<>();
		needRemoveForbiddenList = new ArrayList<>();
		ForbiddenBussiness.getAll(forbiddenIpList, forbiddenMacList, forbiddenSpeakIpList, forbiddenSpeakMacList);
		forbiddenPlatformUserIdMap = PlatformUserIdInfoBussiness.getForbiddenPlatformUserIdList();
		forbiddenCmdMap = PlatformUserIdInfoBussiness.getCmdForbiddenMap(Integer.valueOf(ConfigHelper.getValue("serverId")));
		return true;
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	/**
	 * 判断是否被禁用命令
	 * @param userId
	 * @param cmdCode
	 * @return
	 */
	public static boolean isForbiddenCmd(long userId, int cmdCode) {
		if (forbiddenCmdMap.containsKey(userId) && forbiddenCmdMap.get(userId).containsKey(cmdCode)) {
			if (System.currentTimeMillis() < forbiddenCmdMap.get(userId).get(cmdCode)) {
				return true;
			}
		}

		return false;
	}

	public static void addForbiddenIp(String[] ipList, String[] macInfos) {
		synchronized (forbiddenIpList) {
			if (ipList != null) {
				for (String forbiddenIp : ipList) {
					forbiddenIpList.add(forbiddenIp);
					needAddForbiddenList.add(new ForbiddenInfo(1, forbiddenIp));
				}
			}
		}
		synchronized (forbiddenMacList) {
			if (macInfos != null) {
				for (String forbiddenMac : macInfos) {
					forbiddenMacList.add(forbiddenMac);
					needAddForbiddenList.add(new ForbiddenInfo(2, forbiddenMac));
				}
			}
		}
	}

	public static void removeForbiddenIp(String[] ipList, String[] macInfos) {
		synchronized (forbiddenIpList) {
			if (ipList != null) {
				for (String forbiddenIp : ipList) {
					forbiddenIpList.remove(forbiddenIp);
					needRemoveForbiddenList.add(new ForbiddenInfo(1, forbiddenIp));
				}
			}
		}
		synchronized (forbiddenMacList) {
			if (macInfos != null) {
				for (String forbiddenMac : macInfos) {
					forbiddenMacList.remove(forbiddenMac);
					needRemoveForbiddenList.add(new ForbiddenInfo(2, forbiddenMac));
				}
			}
		}
	}

    public static String checkForbiddenIp(String ip) {
        return forbiddenIpList.contains(ip) ? "True" : "False";
    }

	public static void addForbiddenSpeakIp(String[] ipList, String[] macInfos) {
		synchronized (forbiddenSpeakIpList) {
			if (ipList != null) {
				for (String forbiddenIp : ipList) {
					forbiddenSpeakIpList.add(forbiddenIp);
					needAddForbiddenList.add(new ForbiddenInfo(3, forbiddenIp));
				}
			}
		}
		synchronized (forbiddenSpeakMacList) {
			if (macInfos != null) {
				for (String forbiddenMac : macInfos) {
					forbiddenSpeakMacList.add(forbiddenMac);
					needAddForbiddenList.add(new ForbiddenInfo(4, forbiddenMac));
				}
			}
		}
	}

	public static void removeForbiddenSpeakIp(String[] ipList, String[] macInfos) {
		synchronized (forbiddenSpeakIpList) {
			if (ipList != null) {
				for (String forbiddenIp : ipList) {
					forbiddenSpeakIpList.remove(forbiddenIp);
					needRemoveForbiddenList.add(new ForbiddenInfo(3, forbiddenIp));
				}
			}
		}
		synchronized (forbiddenSpeakMacList) {
			if (macInfos != null) {
				for (String forbiddenMac : macInfos) {
					forbiddenSpeakMacList.remove(forbiddenMac);
					needRemoveForbiddenList.add(new ForbiddenInfo(4, forbiddenMac));
				}
			}
		}
	}


	/**
	 * 是否封禁
	 * @param platformUserId
	 * @param type
	 * @return false 无封禁
	 */
	public static boolean isPlatformForbidden(String platformUserId, int type) {
		PlatformForbidden platformForbidden = getPlatformForbidden(platformUserId, type);
		if (platformForbidden == null) {
			return false;
		}

		if (System.currentTimeMillis() >= platformForbidden.getForbiddenTime()) {
			// 未封禁
			return false;
		}

		// 封禁
		return true;
	}

	/**
	 * 获取封禁信息
	 * @param platformUserId
	 * @param type
	 * @return
	 */
	public static PlatformForbidden getPlatformForbidden(String platformUserId, int type) {
		if (forbiddenPlatformUserIdMap.containsKey(platformUserId)) {
			if (forbiddenPlatformUserIdMap.get(platformUserId).containsKey(type)) {
				return forbiddenPlatformUserIdMap.get(platformUserId).get(type);
			}
		}
		return null;
	}


	/**
	 * 是否能够登录
	 * @param ip
	 */
	public static boolean canLogin(String ip) {
		if (!StringUtils.isNullOrEmpty(ip)) {
			return !forbiddenIpList.contains(ip);
		}
		return true;
	}

	public static void addPlatformForbidden(PlatformForbidden platformForbidden) {
		String platFormUserId = platformForbidden.getPlatFormUserId();
		if (forbiddenPlatformUserIdMap.containsKey(platFormUserId)) {
			Map<Integer, PlatformForbidden> map = forbiddenPlatformUserIdMap.get(platFormUserId);
			map.put(platformForbidden.getForbiddenType(), platformForbidden);
		} else {
			forbiddenPlatformUserIdMap.put(platformForbidden.getPlatFormUserId(), new HashMap<Integer, PlatformForbidden>() {{
				put(platformForbidden.getForbiddenType(), platformForbidden);
			}});
		}
	}

	public static Map<String, Map<Integer, PlatformForbidden>> getForbiddenPlatformUserIdMap() {
		return forbiddenPlatformUserIdMap;
	}
}
